package com.wwlwxg.drools;

import org.drools.core.base.RuleNameEqualsAgendaFilter;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.kie.api.KieServices;
import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.KieSession;
import org.kie.api.runtime.rule.AgendaFilter;

import java.util.ArrayList;

public class DroolsTest {

	KieSession kieSession;

	@Before
	public void init() {
		System.out.println("this is before test");
		KieServices kieServices = KieServices.Factory.get();
		KieContainer kieContainer = kieServices.newKieClasspathContainer();
		kieSession = kieContainer.newKieSession();
	}

	@Test
	public void test1() {

		Order order = new Order();
		order.setOriginalPrice(30d);

		kieSession.insert(order);
		// 激活规则
		kieSession.fireAllRules();
		// 关闭会话
		kieSession.dispose();

		System.out.println(order.getRealPrice());
	}

	/**
	 * 比较操作符
	 */
	@Test
	public void test2() {
		ComparisonOperatorEntity entity = new ComparisonOperatorEntity();
		entity.setNames("liwen");
		ArrayList<String> list = new ArrayList<>();
		list.add("liwen");
		entity.setList(list);

		kieSession.insert(entity);

		kieSession.fireAllRules();

		kieSession.dispose();
	}

	/**
	 * 执行特定的规则判断
	 */
	@Test
	public void test3() {

		ComparisonOperatorEntity entity = new ComparisonOperatorEntity();
		entity.setNames("liwen");
		ArrayList<String> list = new ArrayList<>();
		list.add("liwen");
		entity.setList(list);

		kieSession.insert(entity);

		kieSession.fireAllRules(
				new RuleNameEqualsAgendaFilter("rule_comparison_matches"));

		kieSession.dispose();
	}

	/**
	 * 测试内置方法--update
	 */
	@Test
	public void test4() {

		Student entity = new Student();
		entity.setAge(5);

		kieSession.insert(entity);

		kieSession.fireAllRules();

		kieSession.dispose();
	}

	/**
	 * 测试内置方法--insert
	 */
	@Test
	public void test5() {

		Student entity = new Student();
		entity.setAge(10);

		kieSession.insert(entity);

		kieSession.fireAllRules();

		kieSession.dispose();
	}















}
